<template>
  <el-card class="spu">
    <!-- 添加spu的按钮 -->
    <el-button
      type="primary"
      :icon="Plus"
      :disabled="!CategoryStore.category3Id"
      @click="ShowAddSpu"
      >添加SPU</el-button
    >
    <!-- spu表格 -->
    <el-table :data="spuList" border v-loading="loading" class="spu">
      <el-table-column type="index" label="序号" width="60" align="center" />
      <el-table-column prop="spuName" label="SPU名称" align="center" />
      <el-table-column prop="description" label="SPU描述" align="center">
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template v-slot="{ row }">
          <el-tooltip content="添加SKU" placement="bottom-end" effect="light">
            <el-button
              size="small"
              type="primary"
              :icon="Plus"
              @click="addSku(row)"
            />
          </el-tooltip>
          <el-tooltip content="修改SPU" placement="bottom-end" effect="light">
            <el-button
              size="small"
              type="primary"
              :icon="Edit"
              @click="showSetSpu(row)"
            />
          </el-tooltip>
          <el-tooltip
            content="查看SKU列表"
            placement="bottom-end"
            effect="light"
          >
            <el-button
              size="small"
              type="info"
              :icon="InfoFilled"
              @click="lookSku(row)"
            />
          </el-tooltip>

          <el-tooltip content="删除SPU" placement="bottom-end" effect="light">
            <div class="spu-del">
              <el-popconfirm
                :title="`您确定要删除${row.spuName}属性吗?`"
                @confirm="delSpu(row)"
              >
                <template #reference>
                  <el-button
                    type="danger"
                    size="small"
                    :icon="Delete"
                  ></el-button>
                </template>
              </el-popconfirm>
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      class="spu"
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9]"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
      @size-change="getSpuList"
      @current-change="getSpuList"
    />

    <!-- 查看Sku弹出框 -->
    <el-dialog v-model="dialogTableVisible" :title="`${spuName}=>SKU列表`">
      <el-table :data="skuList">
        <el-table-column property="skuName" label="名称" width="150" />
        <el-table-column property="price" label="价格" width="200" />
        <el-table-column property="weight" label="重量" />
        <el-table-column label="默认图片">
          <template v-slot="{ row }">
            <img :src="row.skuDefaultImg" class="spu-img" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "SpuList",
};
</script>

<script lang="ts" setup>
import { ref, watch } from "vue";
import { Plus, Edit, Delete, InfoFilled } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import type { SpuList, AddSetSpuListItem } from "@/api/product/models/spuModel";
import { getSpuListApi, delSpuApi } from "@/api/product/spu";
import { getSkuListApi } from "@/api/product/sku";
import { useCategoryStore } from "@/stores/category";
const CategoryStore = useCategoryStore();
const loading = ref(false);
const currentPage = ref(1);
const pageSize = ref(3);
const total = ref(0);

//接收自定义事件
const emits = defineEmits(["isShowAddSpu", "setSpu"]);
const ShowAddSpu = () => {
  emits("isShowAddSpu", 2);
  emits("setSpu", {
    description: "",
    spuImageList: [],
    spuName: "",
    spuSaleAttrList: [],
    tmId: undefined,
  });
};

//spu列表数据
const spuList = ref<SpuList>([]);

//获取spu列表的封装函数
const getSpuList = async () => {
  loading.value = true;
  const res = await getSpuListApi({
    page: currentPage.value,
    limit: pageSize.value,
    category3Id: CategoryStore.category3Id as number,
  });
  loading.value = false;
  ElMessage.success("获取SPU列表成功");
  //更改总数
  total.value = res.total;
  // 更改spu列表数据
  spuList.value = res.records;
};

// //改变了每页数量触发的回调
// const handleSizeChange = (newPageSize: number) => {
//   getSpuList();
// };
// //改变了页码触发的回调
// const handleCurrentChange = (newCurren: number) => {
//   getSpuList();
// };

//需要监视3id变化,然后发请求
watch(
  () => CategoryStore.category3Id,
  (newVal) => {
    //没有3id需清空数据
    if (!newVal) {
      spuList.value = [];
      currentPage.value = 1;
      pageSize.value = 3;
      total.value = 0;
      return;
    }
    //有3id发送请求
    getSpuList();
  },
  {
    immediate: true,
  }
);

//切换到修改页面
const showSetSpu = (row: AddSetSpuListItem) => {
  emits("isShowAddSpu", 2);
  emits("setSpu", {
    ...row,
    spuImageList: [],
    spuSaleAttrList: [],
  });
};

//删除Spu
const delSpu = async (row: AddSetSpuListItem) => {
  await delSpuApi(row.id as number);
  ElMessage.success("删除SPU成功");
  //重新渲染页面
  getSpuList();
};

//添加Sku
const addSku = (row: AddSetSpuListItem) => {
  emits("isShowAddSpu", 3);
  emits("setSpu", row);
};

//查看sku
const dialogTableVisible = ref(false);
const skuList = ref([]);
const spuName = ref("");
const lookSku = async (row: AddSetSpuListItem) => {
  dialogTableVisible.value = true;
  //发请求获取sku列表
  skuList.value = await getSkuListApi(row.id as number);
  spuName.value = row.spuName;
};
</script>

<style scoped>
.spu {
  margin-top: 20px;
}
.spu-del {
  display: inline-block;
  margin-left: 10px;
}
.spu-img {
  width: 100px;
}
</style>
